package cn.knowei.day2.jdbc;

import java.io.FileInputStream;
import java.io.IOException;
import java.sql.*;
import java.util.Properties;

public class DBUtils {
    private static String driver;
    private static String url;
    private static String user;
    private static String password;
    static {
        Properties properties = new Properties();

        try {
            properties.load(new FileInputStream("db.properties"));
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            System.out.println("成功");
        }

        driver = properties.getProperty("jdbc.driverClassName");
        url = properties.getProperty("jdbc.url");
        user = properties.getProperty("jdbc.username");
        password = properties.getProperty("jdbc.password");

        try {
            Class.forName(driver);
        } catch (ClassNotFoundException e) {
            e.printStackTrace();
        }
    }

    public static Connection getConnection() throws SQLException {
        return DriverManager.getConnection(url, user, password);
    }

    public static void close(Connection con, Statement stmt, ResultSet rs) throws SQLException {
        rs.close();
        stmt.close();
        con.close();
    }
}
